from utils.prettyrepr import PrettyMeta
from utils.copy import smartcopy
from utils.call import curry

from .path import Path


class Movable1D(object):
    """An object that can move along a single axis. It has a position, speed, and size (for 
    collision detection)."""
    __metaclass__ = PrettyMeta

    def __init__(self, axis, time=0.0, position=0.0, speed=1.0, size=1.0):
        self.axis = axis
        self.size = size
        self.speed = speed
        self.path = Path(time, position)
        
    def __info__(self):
        return ("axis=%s, t=%s, p=%s, speed=%s, size=%s" % 
                (self.axis, self.time, self.position, self.speed, self.size))
        
    __deepcopy__ = curry(smartcopy, deepcopy_attrs=("axis", "size", "speed", "path"))
    
    def set_initial_position(self, time=0.0, position=0.0):
        self.path.clear(time, position)
        
    @property
    def position(self):
        return self.path.position
        
    @property
    def time(self):
        return self.path.time
        
    def time_to(self, position):
        return abs(position - self.path.position) / float(self.speed)
        
